<?php
/* SVN FILE: $Id: app_controller.php 2951 2006-05-25 22:12:33Z phpnut $ */
/**
 * Short description for file.
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP :	Rapid Development Framework <http://www.cakephp.org/>
 * Copyright (c)	2006, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright (c) 2006, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
 * @package			cake
 * @subpackage		cake.cake
 * @since			CakePHP v 0.2.9
 * @version			$Revision: 2951 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2006-05-25 17:12:33 -0500 (Thu, 25 May 2006) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * This is a placeholder class.
 * Create the same file in app/app_controller.php
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		cake
 * @subpackage	cake.cake
 */
class AppController extends Controller {
	
	var $components = array('Email', 'RequestHandler');
	
	function checkSession() {
		if (!$this->Session->check('current_user')) {
			return false;
		} else {
			return true;
		}
	}
	
	function checkLevel($level) {
		if ($this->userInfo['auth_type'] == AUTH_ADMIN) {
			return true;
		} else if ($this->userInfo['auth_type'] == $level) {
			return true;
		} else {
			return false;
		}
	}
	
	function mustBe($group) {
		if ($this->checkSession()) {
			if($this->checkLevel($group)) {// must be $group where $group is one of the AUTH_ constants
			} else {
				$this->notAuth();
			}
		} else {
			$this->forceLogin();
		}
	}
	
	function adminOnly() {
		$this->mustBe(AUTH_ADMIN);
	}
	
	function isMe($username) {
		if ($this->userInfo['username'] == $username) {
			return true;
		} else {
			return false;
		}
	}
	
	function notAuth($message = null) {
		if ($message === null) {
			$message = 'You are not authorized to do that!';
		}
		$this->Session->setFlash($message,'not_auth');
		$this->redirect('/members/menu');
		exit();
	}
	
	function beforeFilter() {
		App::import('model', 'Delivery');
		$del = new Delivery();
		$dId = $del->currentId();
		$this->set('current_delivery', $dId);
		$this->deliveryId = $dId;
		if($this->checkSession()){
			$this->userInfo = $this->Session->read('current_user');
			$this->set('current_user',$this->userInfo);
			if ($this->Session->check('current_basket')) {
				$this->set('current_basket', $this->Session->read('current_basket'));
			}
		} else {
			$this->set('current_user', array('auth_type' => 'guest', 'member_id' => false, 'board_mem'=>false));
		}
		$this->RequestHandler->setContent('json','text/x-json');
		if (isset($this->params['named']['callback'])) {
			$this->set('callback', $this->params['named']['callback']);
			unset($this->params['named']['callback']);
		}
	}
	
	function forceLogin() {
		$this->redirect('/members/login');//force login
		exit();
	}
}
?>